﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class EditInformation : BasePage
    {
        private ExcuteDao InformationManage = new ExcuteDao();
        public string rtnbtn = string.Empty;
        public bool isGiftFlag = false;
        public bool isMenu = false;
        /// <summary>
        /// 页面初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();

            _FileBrowser.BasePath = "../ckfinder/";

            _FileBrowser.SetupCKEditor(hfEditor);

            if (!string.IsNullOrEmpty(Request.QueryString["isGift"]))
            {
                isGiftFlag = true;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["menu"]))
            {
                isMenu = true;
            }
            if (!IsPostBack)
            {
                rtnbtn = "<input type=\"button\" id=\"btnBack\" value=\"返回\" onclick=\"javascript:Index.Open('Customer/InformationList.aspx?mid=" + Request.QueryString["mid"] + "');\" />";

                this.btnSubmit.Visible = false;
                if (!string.IsNullOrEmpty(Request.QueryString["InformationID"]))
                {
                    Session["UrlType"] = "EditInformation";
                    Session["Edit_InformationID"] = SpecialCharHandler.HtmlDiscode(Request.QueryString["InformationID"]).Replace("&amp;", "&");
                    SetMenu();
                    this.btnSubmit.Visible = true;
                    if (isMenu)
                    {
                        btnSubmit.Text = "保存";
                    }
                    SetInitData();

                }
            }
        }
        private void SetMenu()
        {
            //ddlListMenu.Items.Clear();
            //ddlListMenu.Items.Add(new ListItem("", "-1"));

            //DataTable menulistdt = InformationManage.Get_LeftMenu();
            //for (int i = 0; i < menulistdt.Rows.Count; i++)
            //{
            //    ddlListMenu.Items.Add(new ListItem(menulistdt.Rows[i]["MenuName"].ToString(), menulistdt.Rows[i]["MenuID"].ToString()));
            //}
        }
        /// <summary>
        /// 设置初始化页面信息
        /// </summary>
        private void SetInitData()
        {
            //获取用户信息
            DataTable dtInformation = InformationManage.Get_ListText(Convert.ToInt32(Request.QueryString["InformationID"]));
            if (dtInformation.Rows.Count > 0)
            {

                if (isGiftFlag)
                {
                    txtPonit.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["listpoint"].ToString());
                    txtCount.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["listcount"].ToString());
                    rblTopText_IsUse.SelectedIndex = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["isCanBuy"].ToString()) == "true" ? 0 : 1;
                }

                txtListTitle.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["listTitle"].ToString());
                txtListjianjie.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["listjianjie"].ToString());
                txtListFrom.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["listFrom"].ToString());
                hfEditor.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["listText"].ToString());
                //ddlListMenu.SelectedValue = dtInformation.Rows[0]["MenuId"].ToString();

            }
            //InformationFilter.Information_pwd = dtInformation.Rows[0]["Information_Pwd"].ToString();
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //判断是否保存成功
                bool isOk = false;
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }
                //设置需要添加的数据
                int menuID = -1;
                string point = null;
                string count = null;
                bool isCanBuy = false;
                bool isGift = false;

                if (isGiftFlag)
                {
                    point = txtPonit.Text;
                    count = txtCount.Text;
                    isCanBuy = rblTopText_IsUse.SelectedValue == "1" ? true : false;
                    isGift = true;
                }
                else
                {
                    menuID = Convert.ToInt32(Request.QueryString["mid"]);
                }

                //添加用户信息到数据库中
                if (InformationManage.Update_Information(
                    Convert.ToInt32(Request.QueryString["InformationID"]),
                    SpecialCharHandler.HtmlEncode(txtListTitle.Text),
                    SpecialCharHandler.HtmlEncode(txtListjianjie.Text),
                    SpecialCharHandler.HtmlEncode(txtListFrom.Text),
                    menuID,
                    SpecialCharHandler.HtmlEncode(hfEditor.Text),
                    getCurrentUser().UserID,
                    point,
                    count,
                    isCanBuy,
                    isGift) == -1)
                {
                    isOk = true;
                }

                if (isOk)
                {
                    if (isGiftFlag)
                    {
                        ReturnList("InformationListGift.aspx", "编辑信息成功！");
                    }
                    else
                    {
                        if (isMenu)
                        {
                            ShowMsg("编辑信息成功!", 0);
                        }
                        else
                        {
                            ReturnList("InformationList.aspx?mid=" + Request.QueryString["mid"], "编辑信息成功！");
                        }
                    }
                }
                else
                {
                    ShowMsg("编辑信息失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }
        }

        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtListTitle.Text.TrimEnd()))
            {
                ShowMsg("请输入标题！", 1);
                txtListTitle.Focus();
                return false;
            }
            //if (ddlListMenu.SelectedValue.Equals("-1"))
            //{
            //    ShowMsg("请选择所属菜单！", 1);
            //    txtListjianjie.Focus();
            //    return false;
            //}
            if (string.IsNullOrEmpty(txtListjianjie.Text.TrimEnd()))
            {
                ShowMsg("请输入简介！", 1);
                txtListjianjie.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtListFrom.Text.TrimEnd()))
            {
                ShowMsg("请输入信息来源！", 1);
                txtListFrom.Focus();
                return false;
            }

            return true;
        }
    }
}